package com.c.ctgproject.ui.collect

import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.BatteryHandlerAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityBatteryHandlerListBinding
import com.c.ctgproject.entity.ResultEntity
import com.c.ctgproject.entity.guang.BatteryHandlerEntityV3
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2021/9/10
 *描述: 查询动力电池拆解单 页面 电池拆解单 列表 拆解单列表
 */
class BatteryHandlerListActivity : BaseActivityV2(true), BatteryHandlerAdapter.BatteryHandlerAdapterListener {
    private val mBinding by lazy { ActivityBatteryHandlerListBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var page = 1
    private var search: String = ""

    // 电池状态 0-待入库 1-已入库 2-已拆解 3-已出库 4-拆解中
//    private var dismantleStatus = 4
    //1拆解中 2已完成
    private var dismantle = 1


    private val mList = ArrayList<BatteryHandlerEntityV3.DataBean.ListBean>()
    private val mAdapter by lazy { BatteryHandlerAdapter(this, mList, this) }

    @Subscribe
    fun event(type: PositionEvent) {
        if (type.position != -1) {
            mList.removeAt(type.position)
            mAdapter.notifyItemRemoved(type.position)
        }
    }

    override fun event() {
        getData()
        mBinding.mRecyclerView.adapter = mAdapter
        mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)
    }

    private var searchMine = false
    override fun click() {
        // 电池状态 0-待入库 1-已入库 2-已拆解 3-已出库 4-拆解中
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {
//                "全部" -> {
//                    dismantleStatus = null
//                    page = 1
//                    getData()
//                }
                "拆解中" -> {
                    searchMine = false
                    dismantle = 1
                    page = 1
                    getData()
                }
                "我的" -> {
                    searchMine = true
                    dismantle = 2
                    page = 1
                    getData()
                }
            }
        }

        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }
    }

    private fun getData() {
//        ApiMethods.apiSubscribe(ApiMethods.retrofit.batteryHandlerList(search, dismantleStatus, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
        if (searchMine) {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.myBatteryHandlerList(search, page, dismantle=null), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("拆解完列表:$it")
                val data = getGson().fromJson(it, BatteryHandlerEntityV3::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mRecyclerView.handlerPosition(page)
                }
            })
        } else {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.batteryHandlerListV2(search, page, dismantle), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("拆解完列表:$it")
                val data = getGson().fromJson(it, BatteryHandlerEntityV3::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mRecyclerView.handlerPosition(page)
                }
            })
        }
    }

    //去动力电池拆解
    override fun onItemBtnClick(position: Int) {
        val data = mList[position]
        startActivity<BatteryHandlerOverActivity>(
            MyConstants.POSITION to position,
            MyConstants.DATA to data
//            MyConstants.PB_DISMANTLE_ID to data.pbDismantleId,
//            MyConstants.PB_ID to data.pbId
        )
    }

    override fun deleteItem(position: Int, data: BatteryHandlerEntityV3.DataBean.ListBean) {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.deleteDismantle(data.carDismantleId), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
            MyLog.d("删除结果:$it")
            val result= getGson().fromString<ResultEntity<String>>(it)
            if(result.status==1){
                mList.removeAt(position)
                mAdapter.notifyDataSetChanged()
            }
        })
    }
}